﻿

@using SubstrateHelper.Models;
@using SubstrateHelper.Data;
@inject SubstrateHelperDbContext Db
@inject MessageService MessageService



<div>
    <div class="m-1">
        <span>
            <strong class="text-success">基板型号 </strong>包含：
            <select @bind="SelectedSubstrateType">
                <option value="-1">所有类型</option>
                @if (SubstrateTypes != null)
                {
                    @for (int i = 0; i < SubstrateTypes.Count; i++)
                    {
                        <option value="@i">@SubstrateTypes[i]</option>
                    }
                }

            </select>
        </span>

        <span class="ml-5">
            <strong class="text-success">排产状态 </strong> ：
            <select @bind="ScheduledState">
                <option value="0">未排产电容器</option>
                <option value="1">已排产电容器</option>
                <option value="2">所有</option>
            </select>
        </span>

        <span class="ml-5">
            <strong class="text-success">材料型号 </strong>包含：
            <select @bind="SelectedMaterialType">
                <option value="-1">所有类型</option>
                @if (MaterialTypes != null)
                {
                    @for (int i = 0; i < MaterialTypes.Count; i++)
                    {
                        <option value="@i">@MaterialTypes[i]</option>
                    }
                }

            </select>
        </span>

        <span class="ml-5">
            <strong class="text-success">镀金类型 </strong>包含：
            <select @bind="SelectedPlatedType">
                <option value="-1">所有类型</option>
                @if (PlatedTypes != null)
                {
                    @for (int i = 0; i < PlatedTypes.Count; i++)
                    {
                        <option value="@i">@PlatedTypes[i]</option>
                    }
                }

            </select>
        </span>



        <span class="ml-5"><strong class="text-success">入库日期 </strong>从：<input type="date" @bind="UploadingDateStart"></span>
        <span class="ml-1">至：<input type="date" @bind="UploadingDateEnd"></span>



        <Button class="ml-5" IsOutline="false" Color="Color.Success" @onclick="DoQuery">查  询</Button>
    </div>
    <div class="ml-1 mt-2">
        <span><strong class="text-success">比容 </strong>从：<input type="number" @bind="CPAStart" style="width:50px"></span>
        <span class="ml-1">至：<input type="number" @bind="CPAEnd" style="width:50px"></span>

        <span class="ml-5"><strong class="text-success">耐压 </strong>从：<input type="number" @bind="BDVStart"  style="width:50px"></span>
        <span class="ml-1">至：<input type="number" @bind="BDVEnd"  style="width:50px"></span>

        <span class="ml-5"><strong class="text-success">损耗 </strong>从：<input type="number" @bind="DFStart"  style="width:50px"></span>
        <span class="ml-1">至：<input type="number" @bind="DFEnd"  style="width:50px"></span>

        <span class="ml-5"><strong class="text-success">电容排产批号 </strong>包含：<input type="text" @bind="SchedultingLotNo" style="width:100px"></span>

        <span class="ml-5"><strong class="text-success">溅射基板批号 </strong>包含：<input type="text" @bind="LotNo" style="width:100px"></span>

        <span class="ml-5"><strong class="text-success">裸基板批号 </strong>包含：<input type="text" @bind="NakedLotNo" style="width:100px"></span>
    </div>

    <hr class="bg-primary" style="height:5px;" />
    <div class="container-fluid">
        <ComponentSubstratesQueryResult Items="@SubstrateInfos"/>
    </div>


</div>
<Message />


    @code {

        List<string> _SubstrateTypes = null;
        List<string> SubstrateTypes
        {
            get
            {
                try
                {
                    if (_SubstrateTypes == null)
                    {
                        _SubstrateTypes = Db.CandidateLists.Where(x => x.Category == "SubstrateType").Select(x => x.Candidate).OrderBy(x => x).ToList();

                    }


                    return _SubstrateTypes;
                }
                catch
                {
                    return null;
                }


            }
        }
        int SelectedSubstrateType { get; set; } = -1;

        List<string> _MaterialTypes = null;
        List<string> MaterialTypes
        {
            get
            {
                try
                {
                    if (_MaterialTypes == null)
                    {


                        _MaterialTypes = Db.CandidateLists.Where(x => x.Category == "MaterialType").Select(x => x.Candidate).OrderBy(x => x).ToList();
                    }


                    return _MaterialTypes;
                }
                catch
                {
                    return null;
                }


            }
        }
        int SelectedMaterialType { get; set; } = -1;


        //已镀类型
        List<string> _PlatedTypes = null;
        List<string> PlatedTypes
        {
            get
            {
                try
                {
                    if (_PlatedTypes == null)
                    {


                        _PlatedTypes = Db.CandidateLists.Where(x => x.Category == "PlatedType").Select(x => x.Candidate).OrderBy(x => x).ToList();
                    }


                    return _PlatedTypes;
                }
                catch
                {
                    return null;
                }


            }
        }
        int SelectedPlatedType { get; set; } = -1;

        string NakedLotNo { get; set; }
        string SchedultingLotNo { get; set; }
        string LotNo { get; set; }//溅射基板批号


        int ScheduledState { get; set; } = 0;

        DateTime? UploadingDateStart { get; set; } = null;
        DateTime? UploadingDateEnd { get; set; } = null;

        double? CPAStart { get; set; } = null;
        double? CPAEnd { get; set; } = null;

        double? BDVStart { get; set; } = null;
        double? BDVEnd { get; set; } = null;

        double? DFStart { get; set; } = null;
        double? DFEnd { get; set; } = null;

        List<SubstrateInfo> SubstrateInfos { get; set; }



        private void DoQuery()
        {

            try
            {
                IQueryable<SubstrateInfo> query = Db.SubstrateInfos;

                //是否排产
                if (ScheduledState == 0)//未排产
                {
                    query = query.Where(x => x.SchedulingDate == null);
                }
                else if (ScheduledState == 1)//
                {
                    query = query.Where(x => x.SchedulingDate != null);
                }

                //基板类型
                if (SelectedSubstrateType >= 0)//
                {
                    query = query.Where(x => x.SubstrateType.Contains(SubstrateTypes[SelectedSubstrateType]));
                }

                //材料型号
                if (SelectedMaterialType >= 0)
                {
                    query = query.Where(x => x.SubstrateMaterialType.Contains(MaterialTypes[SelectedMaterialType]));
                }

                //镀金类型
                if (SelectedPlatedType >= 0)
                {
                    query = query.Where(x => x.PlatedType.Contains(PlatedTypes[SelectedPlatedType]));
                }

                //比容
                if (CPAStart != null)
                {
                    query = query.Where(x => x.CPA >= CPAStart.Value);
                }

                if (CPAEnd != null)
                {
                    query = query.Where(x => x.CPA <= CPAEnd.Value);
                }

                //耐压
                if (BDVStart != null)
                {
                    query = query.Where(x => x.BDV >= BDVStart.Value);
                }

                if (BDVEnd != null)
                {
                    query = query.Where(x => x.BDV <= BDVEnd.Value);
                }

                //损耗
                if (DFStart != null)
                {
                    query = query.Where(x => x.DF_max >= DFStart.Value);
                }

                if (DFEnd != null)
                {
                    query = query.Where(x => x.DF_max <= DFEnd.Value);
                }

                //电容排产批号
                if (!string.IsNullOrWhiteSpace(SchedultingLotNo))
                {
                    query = query.Where(x => x.ScheduledLotNo.Contains(SchedultingLotNo));
                }

                //溅射批号
                if (!string.IsNullOrWhiteSpace(LotNo))
                {
                    query = query.Where(x => x.LotNo.Contains(LotNo));
                }

                //裸基板批号
                if (!string.IsNullOrWhiteSpace(NakedLotNo))
                {
                    query = query.Where(x => x.NakedLotNo.Contains(NakedLotNo));
                }

                //入库起始日期
                if (UploadingDateStart != null)
                {
                    query = query.Where(x => x.UploadingDate >= UploadingDateStart.Value);
                }

                //入库结束日期
                if (UploadingDateEnd != null)
                {
                    query = query.Where(x => x.UploadingDate <= UploadingDateEnd.Value);
                }


                //去掉报废的
                query = query.Where(x => x.ScheduledLotNo == null || !x.ScheduledLotNo.StartsWith("报废"));

                SubstrateInfos = SubstrateInfo.CopyList(query.ToList());

                MessageService?.Show(new MessageOption()
                {
                    Content = $"返回{SubstrateInfos.Count}条结果。",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Success
                });
            }
            catch(Exception ex)
            {
                MessageService?.Show(new MessageOption()
                {
                    Content = $"发生错误：{ex.Message}",
                    Icon = "fa fa-info-circle",
                    ShowDismiss = true,
                    Color = Color.Danger
                });
            }

        }


    }
